﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;

public partial class joinus2_in : System.Web.UI.Page
{
    private PostFactory m_PostFactory;
    private IPostService m_PostService;
    private HttpHelper m_HttpHelper;
    private int m_NodeId = 436; //其他合作模式
    private int m_CityNodeId = 43;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_HttpHelper = new HttpHelper();

        if (!IsPostBack)
        {
            NodeVO nodeVO = m_PostService.GetNodeById(m_NodeId);
            NodeVO parentNodeVO = m_PostService.GetNodeById(nodeVO.ParentNode.NodeId);
            if (nodeVO != null)
            {
                ltlTitle.Text = string.Format("{0}-{1}", parentNodeVO.Name, nodeVO.Name);
                ltlContent.Text = nodeVO.HtmlContent;
            }

            initCity();
        }
    }
    private void initCity()
    {
        ddlCity.Items.Clear();
        ddlCountry.Items.Clear();
        IList<NodeVO> subNodeList = m_PostService.GetNodeListByParentId(m_CityNodeId);
        ddlCity.Items.Add(new ListItem("請選擇縣市", ""));
        ddlCountry.Items.Add(new ListItem("請選擇鄉鎮市區", ""));
        if (subNodeList != null && subNodeList.Count > 0)
        {
            foreach (NodeVO nodeVO in subNodeList)
            {
                ddlCity.Items.Add(new ListItem(nodeVO.Name, nodeVO.NodeId.ToString()));
            }
        }
    }

    protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        string city = ddlCity.SelectedValue;
        if (string.IsNullOrEmpty(city))
        {
            ddlCountry.Items.Clear();
            ddlCountry.Items.Add(new ListItem("請選擇鄉鎮市區", ""));
        }
        else
        {
            ddlCountry.Items.Clear();
            ddlCountry.Items.Add(new ListItem("請選擇鄉鎮市區", ""));
            IList<NodeVO> subNodeList = m_PostService.GetNodeListByParentId(int.Parse(city));
            if (subNodeList != null && subNodeList.Count > 0)
            {
                foreach (NodeVO nodeVO in subNodeList)
                {
                    ddlCountry.Items.Add(new ListItem(nodeVO.Name, nodeVO.HtmlContent));
                }
            }
        }
    }

    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        string countryZip = ddlCountry.SelectedValue;
        if (string.IsNullOrEmpty(countryZip))
        {
            txtAddressZip.Text = string.Empty;
        }
        else
        {
            txtAddressZip.Text = countryZip;
        }
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        PostVO postVO = new PostVO();
        UIHelper.FillVO(PanelUI, postVO);
        postVO.Node = m_PostService.GetNodeById(m_NodeId);

        ////機構屬性
        List<string> customField1 = new List<string>();
        foreach (ListItem li in ckblCustomField1.Items)
        {
            if (li.Selected)
            {
                customField1.Add(li.Value);
            }
        }
        postVO.CustomField1 = string.Join(";", customField1.ToArray());

        ////預計可提供服務之人力
        List<string> serviceType = new List<string>();
        if (ckbServiceType1.Checked || !string.IsNullOrEmpty(txtServiceType1.Text))
        {
            serviceType.Add(string.Format("{0} {1} 人", ckbServiceType1.Text, txtServiceType1.Text));
        }
        if (ckbServiceType2.Checked || !string.IsNullOrEmpty(txtServiceType2.Text))
        {
            serviceType.Add(string.Format("{0} {1} 人", ckbServiceType2.Text, txtServiceType2.Text));
        }
        if (ckbServiceType3.Checked || !string.IsNullOrEmpty(txtServiceType3.Text))
        {
            serviceType.Add(string.Format("{0} {1} 人", ckbServiceType3.Text, txtServiceType3.Text));
        }
        if (ckbServiceType4.Checked || !string.IsNullOrEmpty(txtServiceType4.Text))
        {
            serviceType.Add(string.Format("{0} {1} 人", ckbServiceType4.Text, txtServiceType4.Text));
        }
        if (ckbServiceType5.Checked || !string.IsNullOrEmpty(txtServiceType5.Text))
        {
            serviceType.Add(string.Format("{0} {1} 人", ckbServiceType5.Text, txtServiceType5.Text));
        }
        if (ckbServiceTypeOther.Checked || !string.IsNullOrEmpty(txtServiceTypeOther.Text))
        {
            serviceType.Add(txtServiceTypeOther.Text);
        }
        postVO.ServiceType = string.Join(";", serviceType.ToArray());

        ////服務對象
        List<string> serviceTarget = new List<string>();
        foreach (ListItem li in ckblServiceTarget.Items)
        {
            if (li.Selected)
            {
                serviceTarget.Add(li.Value);
            }
        }
        if (ckbServiceTargetOther.Checked || !string.IsNullOrEmpty(txtServiceTargetOther.Text))
        {
            serviceTarget.Add(txtServiceTargetOther.Text);
        }
        postVO.ServiceTarget = string.Join(";", serviceTarget.ToArray());

        if (!string.IsNullOrEmpty(ddlCity.SelectedValue))
        {
            postVO.AddressCity = ddlCity.SelectedItem.Text;
        }
        if (!string.IsNullOrEmpty(ddlCountry.SelectedValue))
        {
            postVO.AddressCountry = ddlCountry.SelectedItem.Text;
        }

        postVO.CreatedDate = DateTime.Now;
        postVO.CreatedBy = m_HttpHelper.GetUserIp(Context);
        postVO.Flag = 1;        
        postVO = m_PostService.CreatePost(postVO);
        
        ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("已送出。", "joinus2_in.aspx"), false);
    }
}